package com.jnevision.deptovertimefee.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.jnevision.deptovertimefee.model.DeptOvertimeFee;
import com.jnevision.deptovertimefee.service.DeptOvertimeFeeServiceImpl;

/**
 * <p>
 * 部门加班费用设置 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-26
 */

@RestController
@RequestMapping("/deptovertimefees")
public class DeptOvertimeFeeController extends ApiController {
    @Autowired
    private DeptOvertimeFeeServiceImpl deptOvertimeFeeServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody DeptOvertimeFee overfee) {

        DeptOvertimeFee one = this.deptOvertimeFeeServiceImpl
            .getOne(new QueryWrapper<DeptOvertimeFee>().eq("dept_id", overfee.getDeptId()));
        if (null != one) {
            one.setUnitPrice(overfee.getUnitPrice());
            return this.success(this.deptOvertimeFeeServiceImpl.updateById(one));
        } else {
            return this.success(this.deptOvertimeFeeServiceImpl.save(overfee));
        }

    }

    @PutMapping
    public R<Boolean> update(@RequestBody DeptOvertimeFee overfee) {
        return this.success(this.deptOvertimeFeeServiceImpl.updateById(overfee));
    }

    @GetMapping
    public R<DeptOvertimeFee> get(int id) {
        return this.success(this.deptOvertimeFeeServiceImpl.getById(id));
    }

    @GetMapping("/dept/{deptId}")
    public R<DeptOvertimeFee> getByDeptId(@PathVariable int deptId) {
        List<DeptOvertimeFee> list = this.deptOvertimeFeeServiceImpl
            .list(new QueryWrapper<DeptOvertimeFee>().eq("dept_id", deptId).orderByDesc("id"));

        if (CollectionUtils.isEmpty(list)) {
            return this.success(new DeptOvertimeFee());
        }

        return this.success(list.get(0));
    }
}
